package com.itheima.health.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/mobile/setmeal")
@Slf4j
public class SetMealController {

    @Reference
    private SetMealService setMealService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 查询套餐
     *
     * @return
     */
    @GetMapping("/getSetmeal")
    public Result getSetmeal() {
        ObjectMapper objectMapper = new ObjectMapper();
        List<Setmeal> setmeals = new ArrayList<>();
        try (Jedis jedis = jedisPool.getResource();) {
            String jsonsetmeals = null;
            if (jedis.exists("setmeals")) {
                jsonsetmeals = jedis.get("setmeals");
                log.info("[从redis获取数据-----data:{}]", jsonsetmeals);
                setmeals = objectMapper.readValue(jsonsetmeals, List.class);
                return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, setmeals);
            }
            //rpc调用查询所有套餐
            setmeals = setMealService.findAll();
            jsonsetmeals = objectMapper.writeValueAsString(setmeals);
            jedis.set("setmeals", jsonsetmeals);
            log.info("[从数据库中查询，并存到redis-----data:{}]", jsonsetmeals);
        } catch (Exception e) {
            log.error("", e);
        }
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, setmeals);
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {

        ObjectMapper objectMapper = new ObjectMapper();
        Setmeal setmeal = null;
        String setmeal_id = "setmeal_detail:" + "setmeal" + id;
        String jsonSetmealDetail = null;
        try (Jedis jedis = jedisPool.getResource();) {

            if (jedis.exists(setmeal_id)) {
                jsonSetmealDetail = jedis.get(setmeal_id);
                setmeal = objectMapper.readValue(jsonSetmealDetail, Setmeal.class);
                log.info("[从redis获取数据-----data:{}]", jsonSetmealDetail);
                return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);
            }
            setmeal = setMealService.findById(id);
            jsonSetmealDetail = objectMapper.writeValueAsString(setmeal);
            jedis.set(setmeal_id, jsonSetmealDetail);
            log.info("[从数据库中查询，并存到redis-----data:{}]", jsonSetmealDetail);
        } catch (Exception e) {
            log.error("", e);
        }
        //返回
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);
    }
}